﻿using FastIOC.Annotation;
using FastUtil.Extra;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace FastExecutor.Base.Util
{
    public class CommonUtil
    {
        private static string TokenPrefix = System.Configuration.ConfigurationManager.AppSettings["TokenPrefix"];
        public static string CreateCommonGuid()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss") + Guid.NewGuid().ToString().Substring(0, 20);
        }
        public static bool IsAssignableFrom(Type @Type, Type @BaseType)
        {
            bool Flag = false;
            if (!@BaseType.IsGenericType)
            {
                Flag = @BaseType.IsAssignableFrom(@Type);

            }
            else
            {
                Type[] Interfaces = @Type.GetInterfaces();
                foreach (Type item in Interfaces)
                {
                    if (item.IsGenericType)
                    {
                        var t = item.GetGenericTypeDefinition();
                        if (t == @BaseType)
                        {
                            Flag = true;
                            break;
                        }
                    }
                }
            }

            return Flag;
        }
        public static string GetRootPath()
        {
            //协议
            string Scheme = HttpContext.Current.Request.Url.Scheme;
            //域名
            string Host = HttpContext.Current.Request.Url.Host;
            //端口
            int Port = HttpContext.Current.Request.Url.Port;
            //工程名
            string ApplicationPath = HttpContext.Current.Request.ApplicationPath;
            string RootPath = string.Empty;
            if (Port == 80)
            {
                RootPath = Scheme + "://" + Host + ApplicationPath + "/";
            }
            else
            {
                RootPath = Scheme + "://" + Host + ":" + Port + ApplicationPath + "/";
            }
            return RootPath;
        }

        public static string GetClientIP()
        {
            //获取本地的IP地址
            string userHostAddress = HttpContext.Current.Request.UserHostAddress;

            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            //最后判断获取是否成功，并检查IP地址的格式
            if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
            {
                return userHostAddress;
            }
            return "127.0.0.1";
        }

        /// <summary>
        /// 检查IP地址格式
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string IP)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(IP, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
        public static string GetClientBrowser()
        {
            return System.Web.HttpContext.Current.Request.Browser.Browser;
        }
        public static int GetClientBrowserVersion()
        {
            return System.Web.HttpContext.Current.Request.Browser.MajorVersion;
        }
        public static string GetClientSystem()
        {
            return System.Web.HttpContext.Current.Request.Browser.Platform;
        }


    }
}
